import {
  CommandType,
  IUniverInstanceService,
  UniverInstanceType,
  Workbook,
  IAccessor
} from "@univerjs/core";
import {
  SheetsSelectionsService,
} from "@univerjs/sheets";
import "@univerjs/sheets/facade";
import "@univerjs/ui/facade";
import "@univerjs/docs-ui/facade";
import "@univerjs/sheets-ui/facade";
import "@univerjs/engine-formula/facade";
import "@univerjs/sheets-formula/facade";
import "@univerjs-pro/exchange-client/facade";

export const getRangeProtectionPoint = {
  type: CommandType.OPERATION,
  id: "sheet.command.get-range-protection-point",
  async handler(accessor: IAccessor) {
    const univerInstanceService = accessor.get(IUniverInstanceService);
    const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
    const sheetSelectionManagerService = accessor.get(SheetsSelectionsService);
    const ranges = sheetSelectionManagerService.getCurrentSelections().map(selection => selection.range);
    const sheet = workbook.getActiveSheet();
    let arr = [];
    ranges.map(item => {
      let range = sheet.getRange(item.startRow, item.startColumn, item.endRow, item.endColumn);
      console.log(range.getCellRect());
      arr.push({
        rangeType: item.rangeType,
        startRow: item.startRow,
        startColumn: item.startColumn,
        endRow: item.endRow,
        endColumn: item.endColumn,
        rect: {
          x: 0,
          y: 0
        }
      });
    });
    navigator.clipboard.writeText(JSON.stringify(arr)).then(() => {
      alert("复制成功");
    });
    return ranges;
  }
};
